/*****************************************************************************************
This file is a part of Arise library.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

Arise Engine		PhysicObject.cpp

					E-mail:	arise_engine@mail.ru

					Origin by DK Engine 0.019:
					dev (avon.dn.ua@gmail.com), Agressor (agress0r@mail.ru)

created:			19.02.2006
last changes:		11.09.2007
*****************************************************************************************/

#include "stdafx.h"


PhysicObject::PhysicObject()
{
	type			= PHYSIC_VOLUME_UNDEFINED;
	objective		= PHYSIC_VOLUME_OBJECTIVE_DEFAULT;
	collision_group	= COL_GROUP_COLLIDABLE_NON_PUSHABLE;
	scene_object	= 0;
	shape			= 0;
	is_dynamic		= false;
	is_sleeping		= false;	

	D3DXMatrixIdentity(&transformation);
}
PhysicObject::~PhysicObject()
{
	Release();
}


void PhysicObject::Render()
{
	switch(type)
	{
		case PHYSIC_VOLUME_BOX:
		case PHYSIC_VOLUME_SPHERE:
		case PHYSIC_VOLUME_CAPSULE:
				for(UINT i=0; i < d3dx_meshes.size(); i++)
				{
					d3dx_meshes[i]->DrawSubset(0);
				}
			break;

		case PHYSIC_VOLUME_TRIANGLE_MESH:
		case PHYSIC_VOLUME_CONVEX_MESH:
				for(UINT i=0; i < physic_meshes.size(); i++)
				{
					physic_meshes[i]->Render();
				}
			break;

		default:
			break;

	};
}


void PhysicObject::Init()
{
}


void PhysicObject::Update()
{
}


void PhysicObject::Release()
{
	SAFE_DELETE(shape);

	switch(type)
	{
			case PHYSIC_VOLUME_BOX:
			case PHYSIC_VOLUME_SPHERE:
			case PHYSIC_VOLUME_CAPSULE:
					for(UINT i=0; i < d3dx_meshes.size(); i++)
					{
						d3dx_meshes[i]->Release();
					}
					d3dx_meshes.clear();
				break;

			case PHYSIC_VOLUME_TRIANGLE_MESH:
			case PHYSIC_VOLUME_CONVEX_MESH:				
				for(UINT i=0; i < physic_meshes.size(); i++)
					{
						physic_meshes[i]->Release();
						delete physic_meshes[i];
					}
					physic_meshes.clear();
				break;

			default:
				break;

	};
}


void PhysicObject::ApplyChildTransformation(const D3DXMATRIXA16 &transformation)
{
	if(scene_object)
	{
		scene_object->ApplyTransformation(transformation);
	}

}